import time

from system.models import AllLevel, User

#接口
from django.http import HttpResponse,JsonResponse
from django.views.decorators.csrf import csrf_exempt

from system.views import SUCCESS_CODE,ERR_CODE,ERR_TIMEOUT,SUCCESS_MESSAGE,ERR_MESSAGE

# 添加角色等级
@csrf_exempt
def addLevel(request):
    dic = {}
    if request.method == "POST":
        actionsUser = request.POST.get('actionsUser')
        if User.objects.filter(username=actionsUser)[0].level == '3':
            name = request.POST.get('name')
            if name:
                level = AllLevel.objects.filter(name=name)
                if level:
                    dic['code'] = ERR_CODE
                    dic['message'] = '该角色权限已存在'
                else:
                    level = AllLevel()
                    level.name = name
                    level.gmt_create = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
                    level.gmt_modify = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
                    level.save()

                    dic['code'] = SUCCESS_CODE
                    dic['message'] = '添加成功'
            else:
                dic['code'] = ERR_CODE
                dic['message'] = '数值不能为空'
        else:
            dic['code'] = ERR_CODE
            dic['message'] = '没有操作权限'
    else:
        dic['code'] = ERR_CODE
        dic['message'] = ERR_MESSAGE
    return JsonResponse(dic, json_dumps_params={'ensure_ascii': False})


# 删除角色权限等级
@csrf_exempt
def deleteLevel(request):
    dic = {}
    if request.method == "POST":
        id = request.POST.get('id')
        actionsUser = request.POST.get('actionsUser')
        if User.objects.filter(username=actionsUser)[0].level == '3':
            level = AllLevel.objects.filter(id=id)
            if level:
                level[0].delete()
                dic['code'] = SUCCESS_CODE
                dic['message'] = SUCCESS_MESSAGE
            else:
                dic['code'] = ERR_CODE
                dic['message'] = '角色权限不存在'
        else:
            dic['code'] = ERR_CODE
            dic['message'] = '没有操作权限'
    else:
        dic['code'] = ERR_CODE
        dic['message'] = ERR_MESSAGE
    return JsonResponse(dic, json_dumps_params={'ensure_ascii': False})


# 获取角色权限等级列表
@csrf_exempt
def LevelInfo(request):
    dic = {}
    if request.method == "POST":
        info = AllLevel.objects.all()
        if info:
            levelList = []
            for i in info:
                data = {}
                data['id'] = i.id
                data['name'] = i.name
                data['selected'] = False
                levelList.append(data)
            dic['code'] = SUCCESS_CODE
            dic['message'] = SUCCESS_MESSAGE
            dic['data'] = {
                'levelInfo': levelList
            }
        else:
            dic['code'] = ERR_CODE
            dic['message'] = '暂时没有角色权限'
    else:
        dic['code'] = ERR_CODE
        dic['message'] = ERR_MESSAGE
    return JsonResponse(dic, json_dumps_params={'ensure_ascii': False})

"""获取角色权限等级数据"""
@csrf_exempt
def getLevelData(request):
    dic = {}
    if request.method == 'POST':
        info = AllLevel.objects.all()
        if info:
            levelList = []
            for i in info:
                data = {}
                data['id'] = i.id
                data['name'] = i.name
                levelList.append(data)

            dic['code'] = SUCCESS_CODE
            dic['message'] = SUCCESS_MESSAGE
            pageSize = int(request.POST.get('pageSize'))
            page = int(request.POST.get('pageId'))
            minSize = (page - 1) * pageSize
            maxSize = page * pageSize
            if maxSize >= len(levelList):
                dic['data'] = {
                    'levelList': levelList[minSize:],
                    'size': len(levelList)
                }
            else:
                dic['data'] = {
                    'levelList': levelList[minSize: maxSize],
                    'size': len(levelList)
                }
        else:
            dic['code'] = SUCCESS_CODE
            dic['message'] = '暂时没有角色权限'
    else:
        dic['code'] = ERR_CODE
        dic['message'] = ERR_MESSAGE
    return JsonResponse(dic, json_dumps_params={'ensure_ascii': False})


# 修改角色权限
@csrf_exempt
def editLevel(request):
    dic = {}
    if request.method == 'POST':
        actionsUser = request.POST.get('actionsUser')
        if User.objects.filter(username=actionsUser)[0].level == '3':
            level_id = request.POST.get('id')
            name = request.POST.get('name')

            level = AllLevel.objects.filter(id=level_id)[0]
            if level:
                level.name = name
                level.gmt_modify = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
                level.save()

                dic['code'] = SUCCESS_CODE
                dic['message'] = SUCCESS_MESSAGE
            else:
                dic['code'] = ERR_CODE
                dic['message'] = '找不到该角色权限'
        else:
            dic['code'] = ERR_CODE
            dic['message'] = '没有操作权限'
    else:
        dic['code'] = ERR_CODE
        dic['message'] = ERR_MESSAGE
    return JsonResponse(dic, json_dumps_params={'ensure_ascii': False})